package com.liquidcode.cellular;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;

import utility.vec2;

import entities.CPlayer;
import entities.CScrollingSprite;
import entities.CSprite;

public class CMapManager 
{
	// instance
	private static CMapManager mInstance = null;
	private int m_screenWidth;
	private int m_screenHeight;
	private CScrollingSprite m_mapSprite;
	private CScrollingSprite m_mapOverlaySprite;
	private vec2 m_mapPosition = null;
	private float m_fScaleFactor; 
	private float FAR_SCROLL_SPEED = 0.04f;
	private float NEAR_SCROLL_SPEED = 0.08f;

	
	public static CMapManager getInstance()
	{
		if( mInstance == null )
			mInstance = new CMapManager();
		return mInstance;
	}
	
	private CMapManager()
	{
		m_mapSprite = new CScrollingSprite();
		m_mapSprite.setScrollSpeed(NEAR_SCROLL_SPEED);
		m_mapOverlaySprite = new CScrollingSprite();
		m_mapOverlaySprite.setScrollSpeed(FAR_SCROLL_SPEED);
		
		m_mapPosition = new vec2();
	}
	
	public void Update(float dt)
	{
		// set the scroll speed to the speed of the joystick
		m_mapSprite.setScrollDirX(CPlayer.getInstance().getVelX());
		m_mapSprite.setScrollDirY(CPlayer.getInstance().getVelY());
		m_mapSprite.Update(dt);
		
		m_mapOverlaySprite.setScrollDirX(CPlayer.getInstance().getVelX());
		m_mapOverlaySprite.setScrollDirY(CPlayer.getInstance().getVelY());
		m_mapOverlaySprite.Update(dt);
	}
	
	public void RenderStarField(GL10 gl)
	{
		gl.glPushMatrix();
		gl.glLoadIdentity();

		// translate the object
		gl.glTranslatef(m_screenWidth*0.5f, m_screenHeight*0.5f, 0.0f);
		gl.glScalef(m_fScaleFactor, m_fScaleFactor, 1.0f);


		// set the textures
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, m_mapOverlaySprite.getTextureID());
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		// Point to our buffers
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, m_mapOverlaySprite.getTexBuffer());

		// draw
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, m_mapOverlaySprite.getVB());
		gl.glDrawElements(GL10.GL_TRIANGLES, m_mapOverlaySprite.getNumIndices(),
				GL10.GL_UNSIGNED_SHORT, m_mapOverlaySprite.getIB());

		// pop
		gl.glPopMatrix();
	}
	
	public void RenderMap(GL10 gl)
	{
		// render the starfield first
		RenderStarField(gl);
		
		gl.glPushMatrix();
		gl.glLoadIdentity();

		// translate the object
		gl.glTranslatef(m_screenWidth*0.5f, m_screenHeight*0.5f, 0.0f);
		gl.glScalef(m_fScaleFactor, m_fScaleFactor, 1.0f);


		// set the textures
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, m_mapSprite.getTextureID());
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		// Point to our buffers
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, m_mapSprite.getTexBuffer());

		// draw
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, m_mapSprite.getVB());
		gl.glDrawElements(GL10.GL_TRIANGLES, m_mapSprite.getNumIndices(),
				GL10.GL_UNSIGNED_SHORT, m_mapSprite.getIB());

		// pop
		gl.glPopMatrix();
	}
	
	
	// helper functions 
	public void InitMapWithScreen(int w, int h)
	{
		m_screenWidth = w;
		m_screenHeight = h;
		
		m_mapPosition.setX(m_screenWidth*0.5f);
		m_mapPosition.setY(m_screenHeight*0.5f);
		
		float xDiff = m_screenWidth / m_mapSprite.getWidth();
		float yDiff = m_screenHeight / m_mapSprite.getHeight();
		
		if( xDiff > yDiff )
		{
			m_fScaleFactor = xDiff;
		}
		else
		{
			m_fScaleFactor = yDiff;
		}
	}
	
	// accessors
	public CScrollingSprite getMapTexture() { return m_mapSprite; }
	public CScrollingSprite getMapOverlayTexture() { return m_mapOverlaySprite; }
	
}
